#!/bin/bash
# Install Ansible and clone the repository

# Install pip.conf
pip_conf_dir=$HOME/.config/pip
if [ ! -d $pip_conf_dir ]; then
  mkdir -p $pip_conf_dir
fi
pip_conf_file=$pip_conf_dir/pip.conf
if [ ! -f $pip_conf_file ]; then
  echo "[global]" > $pip_conf_file
  echo "index-url = https://pypi.tuna.tsinghua.edu.cn/simple" >> $pip_conf_file
fi

# Install ansible
# if ansible is not installed, install it
if [ ! $(which ansible) ]; then
  pip install ansible
fi

# add .local/bin to PATH
# check if the following line is in the .bashrc file
# export PATH=$HOME/.local/bin:$PATH
# if not, add it to the .bashrc file and source it
if [ ! $(grep -qxF "export PATH=$HOME/.local/bin:$PATH" $HOME/.bashrc) ]; then
    echo 'export PATH=$HOME/.local/bin:$PATH' >> $HOME/.bashrc
    source $HOME/.bashrc
fi

# mkdir for ansible collections
ansible_collections_dir=$HOME/.ansible/collections/ansible_collections
if [ ! -d $ansible_collections_dir ]; then
  mkdir -p $ansible_collections_dir
fi

# Clone the repository to the ansible_collections directory
cd $ansible_collections_dir
# if the repository is not cloned, clone it
if [ ! -d $ansible_collections_dir/luyanfei ]; then
    git clone https://gitee.com/luyanfei/myansible.git luyanfei
fi

